enum PageParams {
  COOKIE = 'cookie',
  COMPANY_ID = 'companyId',
  FARM_ID = 'farmId',
  LAND_ID = 'landId', //地块ID
  USER_TOKEN = 'userToken',
  SEL_LAND = 'selectedLand'
}
export function useQueryParams(route: any) {
  const url = window.location.href;        
  const querys = url.substring(url.indexOf('?') + 1).split('&');
  const keys:any={};
  for(let i=0;i<querys.length;i++){
    const temp:any=querys[i].split('=');
      if(temp.length<2){
        keys[temp[0]]='';
      }
      else{
        keys[temp[0]]=temp[1];
      }
  }
    for(let key in keys){
      setCookie(key,keys[key])
    };
}

export function setCookie(name:any, value:any, daysToExpire:any) {
  const expirationDate:any = new Date();
  expirationDate.setDate(expirationDate.getDate() + daysToExpire);

  const cookieString:any = name + "=" + encodeURIComponent(value) + ';';
  document.cookie = cookieString;
}

export function getCookie(name:any) {
  const cookies = document.cookie.split("; ");
  for (let i = 0; i < cookies.length; i++) {
    let cookie = cookies[i].split("=");
    if (cookie[0] === name) {
      return cookies[i]?.split('=')[1];
    }
  }
  return null;
}

export function deleteCookie(name:any) {
  const item = getCookie(name)
  if (item) {
    document.cookie.replace(item, '')
  }
}

export function updateCookie(name:any, newValue:any, daysToExpire:any) {
  deleteCookie(name); // 先删除旧的Cookie
  setCookie(name, newValue, daysToExpire); // 添加新的Cookie
}

